/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import static persistencia.DBManagerIngresos.conexion;
import modelo.tabla.Ingresos;
import modelo.MetodosIngresos;


/**
 *Panel donde se pintaran las consultas en un JTable
 * @author Kevin
 */
public class JPConsultasIngresos extends javax.swing.JPanel {
     JPCrearIngreso jpcmodelo;
    JPConsultasIngresos jpcconsultas;
    JPBorrarIngreso jpmodeloborrado;
    JPModificarIngreso jpmodelomodificacion;
    ArrayList<Ingresos> al;
    ResultSet rs;

    String[] columns = new String [] {
                "Titulo", "Estilo", "Año", "Estado", "Ubicacion"
            };
    
    String path = "../img/iconosclasificacionenergetica/";
    class MyTableModel extends DefaultTableModel{
        
        // Returning the Class of each column will allow different
        // renderers to be used based on Class
        @Override
        public Class getColumnClass(int column)
        {
            switch(column) {
                case 0:
                case 1: return String.class;
                case 2: return Integer.class;
                case 3: return String.class;
                case 4: return ImageIcon.class;
                default: return Object.class;
            }
        }
    }  
    

    
    
    private void rellenarJTable(ResultSet rs) {
         try {
            MyTableModel dtm = new MyTableModel();
            dtm.setColumnIdentifiers(columns);
            
            Object [] obj;
            while (rs.next()) {
                obj = new Object[5];
                obj[0]= rs.getString("Titulo");
                obj[1]= rs.getString("Estilo");
                obj[2] = rs.getInt("Año");
                obj[3] = rs.getString("Estado");
                obj[4] = rs.getString("Ubicacion");
                
                
                System.out.println("1111");
            }

            jTable1.setModel(dtm);
        } catch (Exception e) {
            e.printStackTrace();
        }       
    }
    /**
     * Creates new form JPConsultas
     */
        public void actualizarIdPacientes() throws SQLException, ClassNotFoundException{
        jComboId.removeAllItems();
        MetodosIngresos mti = new MetodosIngresos();
        al = mti.getIngresos();
        for (int i = 0; i < al.size(); i++) {
            jComboId.addItem(al.get(i).getIdPA()); //+al.get(i).getNombre());
        }
    }
    public JPConsultasIngresos() throws SQLException, ClassNotFoundException /*throws SQLException, ClassNotFoundException*/ {
        initComponents();
        jButtonConsultar.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                //Recoger los campos a consultar y pasarlos al metodo Consulta
                String nIdPA = (String) jComboId.getSelectedItem();
                String nFechaI = (String) jtextFechaI.getText();
                String nFechaA = (String) jtextFechaA.getText();
                MetodosIngresos mti = null;
                mti = new MetodosIngresos();
                try {
                    rs = mti.Consulta(nIdPA, nFechaI, nFechaA);
                    rellenarJTable(rs);
                    System.out.println("CONSULTA REALIZADA");
                } catch (SQLException ex) {
                    Logger.getLogger(JPConsultasIngresos.class.getName()).log(Level.SEVERE, null, ex);
                }
               
                }

        });


    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        jButtonConsultar = new javax.swing.JButton();
        label1 = new java.awt.Label();
        label3 = new java.awt.Label();
        label4 = new java.awt.Label();
        jtextFechaI = new java.awt.TextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jComboId = new javax.swing.JComboBox();
        jtextFechaA = new java.awt.TextField();

        setLayout(new java.awt.GridBagLayout());

        jButtonConsultar.setText("Consultar");
        jButtonConsultar.setToolTipText("");
        jButtonConsultar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonConsultarActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(18, 38, 0, 0);
        add(jButtonConsultar, gridBagConstraints);

        label1.setText("Id del Paciente:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 50, 0, 0);
        add(label1, gridBagConstraints);
        label1.getAccessibleContext().setAccessibleName("Producto: ");

        label3.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        label3.setName(""); // NOI18N
        label3.setText("Fecha de Ingreso:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(10, 32, 0, 0);
        add(label3, gridBagConstraints);
        label3.getAccessibleContext().setAccessibleName("");

        label4.setText("Fecha de Alta:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(10, 54, 0, 0);
        add(label4, gridBagConstraints);

        jtextFechaI.setMinimumSize(new java.awt.Dimension(300, 20));
        jtextFechaI.setName("emisionesText"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(10, 120, 0, 0);
        add(jtextFechaI, gridBagConstraints);

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "IdIngreso", "IdPaciente", "Fecha entrada", "Fecha de Alta"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        jScrollPane1.setViewportView(jTable1);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.gridwidth = 6;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipadx = 609;
        gridBagConstraints.ipady = 250;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(6, 10, 12, 0);
        add(jScrollPane1, gridBagConstraints);

        jComboId.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.ipadx = 244;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 120, 0, 0);
        add(jComboId, gridBagConstraints);

        jtextFechaA.setMinimumSize(new java.awt.Dimension(300, 20));
        jtextFechaA.setName("emisionesText"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(10, 120, 0, 0);
        add(jtextFechaA, gridBagConstraints);
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonConsultarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonConsultarActionPerformed

    }//GEN-LAST:event_jButtonConsultarActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonConsultar;
    private javax.swing.JComboBox jComboId;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private java.awt.TextField jtextFechaA;
    private java.awt.TextField jtextFechaI;
    private java.awt.Label label1;
    private java.awt.Label label3;
    private java.awt.Label label4;
    // End of variables declaration//GEN-END:variables
}
